package araujoCoronelVillamayor.plantasVsZombies.uiWicket;

import araujoCoronelVillamayor.plantasVsZombies.appModelsArena.SesionAppModel
import araujoCoronelVillamayor.plantasVsZombies.appModelsArena.JardinZenAppModel
import araujoCoronelVillamayor.plantasVsZombies.uiWicket.layouts.LayoutPrincipal
import org.apache.wicket.markup.html.basic.Label
import org.apache.wicket.model.CompoundPropertyModel
import org.uqbar.wicket.xtend.WicketExtensionFactoryMethods
import org.uqbar.wicket.xtend.XLink
import org.uqbar.wicket.xtend.XListView
import araujoCoronelVillamayor.plantasVsZombies.domain.Fila

/**
 * Homepage
 */
class JardinPage extends LayoutPrincipal {
	extension WicketExtensionFactoryMethods = new WicketExtensionFactoryMethods
	
	new() {
		super();
		
		defaultModel = new CompoundPropertyModel(new SesionAppModel())

        this.addChild(new Label("nombreJugador"))
        this.addChild(new Label("recursos"))
        
        this.addChild(new XLink("almanaqueZombie")=> [
            onClick = [| responsePage = new AlmanaquePage(this) ]
        ])      
            
        this.addChild(new XLink("jardinZen") => [
			onClick = [| irJardinZen(sesion.jardinZenAppModel) ]
        ])
		//-------------------------------------------------------------------------------------//
		//----------------------------------- GRILLA JARDIN -----------------------------------//
		//-------------------------------------------------------------------------------------//
		val listView = new XListView("resultados") //resultados es el id de la tr
		listView.model = sesion.jardin.filas.asCompoundModel //no pude acceder desde defaultModel
		listView.populateItem = [ item |
			item.model = item.modelObject.asCompoundModel
			item.addChild(new Label("tipo"));
			item.addChild(new Label("cantidadPlantas"));
			item.addChild(new Label("casillerosPlanta"));
			//Botones para Plantar y Atacar
			item.addChild(new XLink("plantar") => [
				onClick = [| irPlantarPage(item.modelObject) ]
        	])
			item.addChild(new XLink("atacar") => [
				onClick = [| atacar(item.modelObject) ]
        	])	
		]
		this.addChild(listView);
		//-------------------------------------------------------------------------------------//
		//----------------------------------- GRILLA JARDIN -----------------------------------//
		//-------------------------------------------------------------------------------------//
    }
 
	def void irPlantarPage(Fila filaParametro) {
		responsePage = new PlantacionPage(filaParametro, sesion, this)
	}

	def irJardinZen(JardinZenAppModel jardinZenAppModelparametro) {
		responsePage = new JardinZenPage(jardinZenAppModelparametro, this) 
	}		

	def atacar(Fila filaParametro) {
		responsePage = new AtaquePage(filaParametro, sesion, this) 
	}
	
 	def getSesion() {
		this.getDefaultModelObject() as SesionAppModel;
	}
	
	def refreshFilas() {
		this.sesion.refresh()
	}
}
